﻿/**
 * Created by jocky on 07/10/2017.
 */
$().ready(function () {
    //入金历史
    rechargesObj.history();

    //入金提示
    var PAY_RECHARGE_SN = getCookie('PAY_RECHARGE_SN');
    if (PAY_RECHARGE_SN) {
        rechargesObj.info();
    }
});

/**
 * 获取指定的cookie
 *
 * @param name
 * @returns {string}
 */
function getCookie(name) {
    if (document.cookie.length > 0) {
        var start = document.cookie.indexOf(name + "=");
        if (start != -1) {
            start = start + name.length + 1;
            var end = document.cookie.indexOf(";", start);
            if (end == -1) end = document.cookie.length;
            return document.cookie.substring(start, end);
        }
    }

    return ""
}

var rechargesObj = {
    /**
     * 提交
     */
    submit: function () {
        var form = $("#form_moneyRecharge");
        var params = {};
        var validFlag = true;
        $.each(form.find("select,input"), function (i, v) {
            if (v.name != "") {
                var val = $.trim(v.value);
                console.log(val);
                if (!val || val == 0) {
                    $(v).focus();
                    setNotify($(v).attr('data-error'), true);

                    validFlag = false;
                    return false;
                }
                params[v.name] = val;
            }
        });

        if (!validFlag) {
            return false;
        }
        $.post('/moneys/recharge-submit', params, function (json) {
            if (json.code != 1) {
                setNotify(json.msg, true);
            } else {
                //跳转到支付页面
                if (params['payType'] == '1') { //网关支付
                    window.location.href = 'moneys/recharge-pay-form?rechargeSN=' + json.rechargeSN+'&bank='+params['bank'];
                } else { //快捷支付
                    window.location.href = 'moneys/recharge-pay-form?rechargeSN=' + json.rechargeSN;
                }
            }
        }, 'json');

    },

    /**
     * 入金提醒
     */
    info: function () {
        $.getJSON('/moneys/recharge-pay-info', function (json) {
            var status = json.status;
            var msg = "";
            if (status != 2) {
                msg = '入金失败 | ' + json.code;
            } else {
                msg = '入金成功';
            }

            var obj = {
                title: '提示',
                text: msg,
                styling: 'bootstrap3',
                delay: 5000,
                type: status == 2 ? 'info' : 'error'
            };
            new PNotify(obj);
        })
    },

    /**
     * 入金历史
     */
    history: function (type, direction) {
        //显示loading
        show_loading_table('moneyRechargeHistory_table', 5);

        var params = {};

        var pageNo_dom = 'moneyRechargeHistory_pageNo';
        var pageTotal_dom = 'moneyRechargeHistory_pageTotal';
        params.pageNo = getPageNo(pageNo_dom, pageTotal_dom, type, direction);

        var formObj = $('#form_moneyRechargeHistory');
        $.each(formObj.find('input,select'), function (i, v) {
            var name = $.trim($(v).attr('name'));
            if (name != '') {
                params[name] = $(v).val();
            }
        });

        $.getJSON('/moneys/recharge-history', params, function (json) {
            var t = json;
            $("#" + pageNo_dom).val(parseInt(t.pageNo));
            $("#" + pageTotal_dom).html(parseInt(t.pageTotal));
            var sumMoney = parseFloat(t.sumMoney).toFixed(2);

            var tr_head = "<tr>" +
                "<td><i style='color: red;'>汇总</i></td>" +
                "<td></td>" +
                "<td></td>" +
                "<td class='text-right'><i>" + sumMoney + "</i></td>" +
                "</tr>";

            var tr = "";
            $.each(t.lists, function (i, v) {
                var status;
                switch (parseInt(v.status)) {
                    case 1:
                        status = "<label class='label label-default'>请求中</label>";
                        break;
                    case 2:
                        status = "<label class='label label-success'>成功</label>";
                        break;
                    case 3:
                        status = "<label class='label label-danger'>失败 | " + v.resp_code + "</label>";
                        break;
                    case 4:
                        status = "<label class='label label-default'>已过期</label>";
                        break;
                    default:
                        status = "";
                        break;
                }
                tr += "<tr>";
                tr += "<td>" + convertDate(v.pay_start_time, 1) + "</td>";
                tr += "<td class='text-center'>" + (v.type == 1 ? "快捷支付" : "网关支付") + "</td>";
                tr += "<td>" + status + "</td>";
                tr += "<td class='text-right'><i>" + parseFloat(v.money) + "</i></td>";
                tr += "</tr>";
            });

            $("#moneyRechargeHistory_table").find('tbody').html(tr_head + tr);

            page_content_onresize();
        })
    },

    /**
     * 支付详情
     * @param id
     */
    detail: function (id) {

    }
};